package com.wac.data.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wac.data.entity.Comment;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author beta
 * @since 2023-03-16
 */
public interface CommentMapper extends BaseMapper<Comment> {

	//获取用户的评论数量
	@Select("select count(*) from `comment` where user_id = #{userId}")
	int getCommentNum(String userId);

	//获取某实体的评论数量
	@Select("select count(*) from `comment` where comment_id = #{id}")
	int getEntityNum(String id);

	//分页查询某实体的评论列表 时间正序 倒序
	@Select("select (@i:=@i+1) AS `row`,result.* " +
			"from (SELECT @i := 0) AS i," +
			"(select `comment`.*,`user`.`name`,`user`.head_url,l.likeNum from `comment` " +
			"left join `user` on  `user`.id = `comment`.user_id,(select `comment`.id,count(entity_id) as likeNum from `comment` " +
			"left join `like` on `like`.entity_id = `comment`.id group by `comment`.id) as l " +
			"where `comment`.comment_id = #{id} and `comment`.id = l.id order by time asc) as result")
	Page<Comment> selectListById(Page page, String id);

	@Select("select (@i:=@i+1) AS `row`,result.* " +
			"from (SELECT @i := 0) AS i," +
			"(select `comment`.*,`user`.`name`,`user`.head_url,l.likeNum from `comment` " +
			"left join `user` on  `user`.id = `comment`.user_id,(select `comment`.id,count(entity_id) as likeNum from `comment` " +
			"left join `like` on `like`.entity_id = `comment`.id group by `comment`.id) as l " +
			"where `comment`.comment_id = #{id} and `comment`.id = l.id order by time desc) as result")
	Page<Comment> selectListByIdDesc(Page page, String id);

	//分页查询某用户的所有评论 时间倒序
	@Select("select `comment`.*,`user`.`name`,`user`.head_url " +
			"from `comment` left join `user` on `user`.id = `comment`.user_id " +
			"where `comment`.user_id = #{userId} order by time desc")
	Page<Comment> selectByUserId(Page page, String userId);

	//根据评论id查询原文
	@Select("select `comment`.*,`user`.`name` from `comment` " +
			"left join `user` on  `user`.id = `comment`.user_id where `comment`.id = #{id}")
	Comment selectById(String id);

	// 插入操作
	@Select("insert into `comment` (user_id,type,comment_id,content) values(#{userId},#{type},#{commentId},#{content})")
	int addComment(String userId, String type, String commentId, String content);

	//删除操作


}
